package confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.GetResponse;
import com.rabbitmq.client.MessageProperties;
import config.RabbitConfig;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 普通等待确认
 *
 * @author tuyou
 * @date 2020-12-19 14:07:30
 */
public class ConfirmTest {

    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {

        try (Connection connection = RabbitConfig.getConnection();
             Channel channel = connection.createChannel()) {

            String queue = channel.queueDeclare().getQueue();
            int count = 10;

            channel.confirmSelect();
            for (int i = 0; i < count; i++) {
                channel.basicPublish("", queue, MessageProperties.PERSISTENT_TEXT_PLAIN, String.valueOf(i).getBytes());
            }

            if (!channel.waitForConfirms()) {
                System.out.println("等待确认失败！");
            }

            for (int i = 0; i < count; i++) {
                GetResponse resp = channel.basicGet(queue, true);
                System.out.println(new String(resp.getBody()));
            }
        }
    }
}
